package com.hutao.medical.service.impl;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.hutao.medical.common.config.ConfigProperties;
import com.hutao.medical.common.config.FunctionType;
import com.hutao.medical.common.util.DateUtil;
import com.hutao.medical.common.util.JsonUtils;
import com.hutao.medical.common.util.StringUtil;
import com.hutao.medical.constant.ModifyConstant;
import com.hutao.medical.dataSource.TargetDataSource;
import com.hutao.medical.mapper.MedicalErrorMapper;
import com.hutao.medical.mapper.MedicalModifyMapper;
import com.hutao.medical.mapper.audit.MedicalRecordAuditMapper;
import com.hutao.medical.mapper.borrow.HospitalNoteMapper;
import com.hutao.medical.mapper.borrow.MedicalFunctionMapper;
import com.hutao.medical.pojo.MedicalModify;
import com.hutao.medical.pojo.borrow.HospitalNote;
import com.hutao.medical.pojo.borrow.MedicalFunction;
import com.hutao.medical.query.AuditQuery;
import com.hutao.medical.query.MedModifyVo;
import com.hutao.medical.service.MedicalModifyService;

@Service
public class MedicalModifyServiceImpl implements MedicalModifyService {

	@Autowired
	private MedicalFunctionMapper medicalFunctionMapper;
	@Autowired
	private HospitalNoteMapper hospitalNoteMapper;
	@Autowired
	private MedicalModifyMapper medicalModifyMapper;
	@Autowired
	private MedicalRecordAuditMapper medicalRecordAuditMapper;
	@Autowired
	private ConfigProperties configProperties;

	@Autowired
	private MedicalErrorMapper medicalErrorMapper;

	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public void addModifyError(List<String> firstGuidList) throws ParseException {

		// 如果查询出的firstGuid为空直接返回
		if (CollectionUtils.isEmpty(firstGuidList)) {
			return;
		}

		for (String firstGuid : firstGuidList) {
			// 获取json文件路径和名称
			String filePath = configProperties.getServer().getHospitalNoteJsonLocalPath()
					+ StringUtil.getLocalPath(firstGuid);
			String fileName = firstGuid + ".json";
			// 读取json文件如果数据为空直接返回
			Map<String, Object> jsonMap = readLineFile(filePath, fileName);
			if (MapUtils.isEmpty(jsonMap)) {
				continue;
			}

			// 获取基本数据信息如果数据为空直接返回
			Map<String, Object> basicInfoMap = (Map<String, Object>) jsonMap.get("basicInfo");
			
			if (MapUtils.isEmpty(basicInfoMap)) {
				continue;
			}
			String inHospitalnDateTime = null;

			// 根据firstGuid读取HospitalNote如果不存在新增,存在更新
			HospitalNote hospitalNote = hospitalNoteMapper.getHospitalNoteByFirstGuid(firstGuid);
			if (hospitalNote == null) {
				hospitalNote = this.appendHospitalNote(firstGuid, basicInfoMap);
				hospitalNoteMapper.insertSelective(hospitalNote);
			} else {
				hospitalNote = this.appendUpdateHospitalNote(firstGuid, basicInfoMap, hospitalNote);
				hospitalNoteMapper.updateByPrimaryKeySelective(hospitalNote);
			}

			// 根据firstGuid和FunctionType查询MedicalFunction如果存在直接返回不存在新增
			MedicalFunction medicalFunction = null;
			if (medicalFunctionMapper.isExistMedFunctionByFirstGuidAndFunType(firstGuid,
					FunctionType.MEDICAL_MODIFY) > 0) {
				medicalFunction = medicalFunctionMapper.getMediFunctionIdFirstGuidAndFunType(firstGuid,
						FunctionType.MEDICAL_MODIFY);
			} else {
				medicalFunction = new MedicalFunction();
				medicalFunction.setCreateTime(new Date());
				medicalFunction.setFirstGuid(firstGuid);
				medicalFunction.setStatus(1);
				medicalFunction.setFunctionTypeId(FunctionType.MEDICAL_MODIFY);
				medicalFunctionMapper.insertSelective(medicalFunction);
			}


			// 确诊时间
			String diagConfirmedDateTime = null;
			if (basicInfoMap.get("diagConfirmedDateTime") != null) {
				diagConfirmedDateTime = (String) basicInfoMap.get("diagConfirmedDateTime");

				this.uploadMedicalModify(ModifyConstant.NULL_DIAG_CONFIRM_TIME_CODE, "确诊时间由空改为" + diagConfirmedDateTime,
						medicalFunction.getId());

			} else {
				this.addMedicalModify(ModifyConstant.NULL_DIAG_CONFIRM_TIME_CODE, medicalFunction.getId());
			}

			// 医疗付款方式
			String medicalPayTypeName = null;
			if (basicInfoMap.get("medicalPayTypeName") != null) {
				medicalPayTypeName = (String) basicInfoMap.get("medicalPayTypeName");

				// 医疗方式为军队医改军事训练伤是否为空
				if (medicalPayTypeName.equals("军队医改")) {
					if (basicInfoMap.get("militaryTrainingInjuryName") != null) {
						this.uploadMedicalModify(ModifyConstant.WHETHER_MILITARY_TRAINING_INJURY,
								"付费方式为军政医改由空改为" + (String) basicInfoMap.get("militaryTrainingInjuryName"),
								medicalFunction.getId());
					} else {
						this.addMedicalModify(ModifyConstant.WHETHER_MILITARY_TRAINING_INJURY, medicalFunction.getId());

					}
				}

				this.uploadMedicalModify(ModifyConstant.NULL_PAY_STYLE, "付费方式由空改为" + medicalPayTypeName,
						medicalFunction.getId());
			} else {
				this.addMedicalModify(ModifyConstant.NULL_PAY_STYLE, medicalFunction.getId());
			}

			// 入院时间
			if (basicInfoMap.get("inHospitalnDateTime") != null) {
				inHospitalnDateTime = (String) basicInfoMap.get("inHospitalnDateTime");
				this.uploadMedicalModify(ModifyConstant.NULL_INHOSPITAL_TIME, "入院时间由空改为" + inHospitalnDateTime,
						medicalFunction.getId());
			} else {
				this.addMedicalModify(ModifyConstant.NULL_INHOSPITAL_TIME, medicalFunction.getId());
			}

			// 出院时间
			String outHospitalDateTime = null;
			if (basicInfoMap.get("outHospitalDateTime") != null) {
				outHospitalDateTime = (String) basicInfoMap.get("outHospitalDateTime");
				this.uploadMedicalModify(ModifyConstant.NULL_OUTHOSPITAL_TIME, "出院时间由空改为" + outHospitalDateTime,
						medicalFunction.getId());
			} else {
				this.addMedicalModify(ModifyConstant.NULL_OUTHOSPITAL_TIME, medicalFunction.getId());
			}

			// 确诊时间小于入院时间
			if (StringUtils.isNotEmpty(diagConfirmedDateTime) && StringUtils.isNotEmpty(inHospitalnDateTime)) {
				if (this.strToDate(diagConfirmedDateTime).getTime() > this.strToDate(inHospitalnDateTime).getTime()) {
					this.uploadMedicalModify(ModifyConstant.CONFIRM_TIME_LOWER_INHOSPITAL_TIME, "确诊时间小于入院时间已修改",
							medicalFunction.getId());
				} else {
					this.addMedicalModify(ModifyConstant.CONFIRM_TIME_LOWER_INHOSPITAL_TIME, medicalFunction.getId());
				}

			}

			// 确诊时间大于出院时间
			if (StringUtils.isNotEmpty(diagConfirmedDateTime) && StringUtils.isNotEmpty(outHospitalDateTime)) {
				if (this.strToDate(diagConfirmedDateTime).getTime() < this.strToDate(outHospitalDateTime).getTime()) {
					this.uploadMedicalModify(ModifyConstant.CONFIRM_TIME_GREATER_THAN_OUTHOSPITAL_TIME, "确诊时间大于出院时间已修改",
							medicalFunction.getId());
				} else {
					this.addMedicalModify(ModifyConstant.CONFIRM_TIME_GREATER_THAN_OUTHOSPITAL_TIME,
							medicalFunction.getId());
				}

			}

			// 出院时间小于入院时间
			if (StringUtils.isNotEmpty(inHospitalnDateTime) && StringUtils.isNotEmpty(outHospitalDateTime)) {
				if (this.strToDate(inHospitalnDateTime).getTime() < this.strToDate(outHospitalDateTime).getTime()) {
					this.uploadMedicalModify(ModifyConstant.OUTHOSPITAL_TIME_LOWER_THAN_INTHOSPITAL_TIME,
							"出院时间小于入院时间已修改", medicalFunction.getId());
				} else {
					this.addMedicalModify(ModifyConstant.OUTHOSPITAL_TIME_LOWER_THAN_INTHOSPITAL_TIME,
							medicalFunction.getId());
				}

			}
			//手术与病理诊断
			String operationAndPathologicalDiagnosis = null;
			//临床与病理诊断
			String clinicalAndPathologicalDiagnosis = null;
			
			@SuppressWarnings("unchecked")
			Map<String, Object> cureInfoMap = (Map<String, Object>) jsonMap.get("cureInfo");
			if (MapUtils.isNotEmpty(cureInfoMap)) {
				@SuppressWarnings("unchecked")
				List<Map<String, Object>> cureWorkers = (List<Map<String, Object>>) cureInfoMap.get("cureWorkers");

				if (CollectionUtils.isNotEmpty(cureWorkers)) {
					for (Map<String, Object> cureWorker : cureWorkers) {
						String professionTitleName = null;

						if (cureWorker.get("professionTitleName") != null) {
							professionTitleName = (String) cureWorker.get("professionTitleName");

							if (professionTitleName.equals("质控医师")) {
								if (cureWorker.get("medicalWorkerName") != null) {
									this.uploadMedicalModify(ModifyConstant.NULL_QUALITY_CONTROL_PHYSICIAN,
											"质控医生由空修改为" + (String) cureWorker.get("medicalWorkerName"),
											medicalFunction.getId());
								} else {
									this.addMedicalModify(ModifyConstant.NULL_QUALITY_CONTROL_PHYSICIAN,
											medicalFunction.getId());
								}
							}

							if (professionTitleName.equals("质控护士")) {
								if (cureWorker.get("medicalWorkerName") != null) {
									this.uploadMedicalModify(ModifyConstant.NULL_QUALITY_CONTROL_NURSE,
											"质控护士由空修改为" + (String) cureWorker.get("medicalWorkerName"),
											medicalFunction.getId());
								} else {
									this.addMedicalModify(ModifyConstant.NULL_QUALITY_CONTROL_NURSE,
											medicalFunction.getId());
								}
							}

							if (professionTitleName.equals("科主任")) {
								if (cureWorker.get("medicalWorkerName") != null) {
									this.uploadMedicalModify(ModifyConstant.NULL_HEAD_OF_DEPARTMENT,
											"科主任由空修改为" + (String) cureWorker.get("medicalWorkerName"),
											medicalFunction.getId());
								} else {
									this.addMedicalModify(ModifyConstant.NULL_HEAD_OF_DEPARTMENT,
											medicalFunction.getId());
								}
							}
							
							if (professionTitleName.equals("住院医师")) {
								if (cureWorker.get("medicalWorkerName") != null) {
									this.uploadMedicalModify(ModifyConstant.NULL_HEAD_OF_DEPARTMENT,
											"经治医师由空修改为" + (String) cureWorker.get("medicalWorkerName"),
											medicalFunction.getId());
								} else {
									this.addMedicalModify(ModifyConstant.NULL_HEAD_OF_DEPARTMENT,
											medicalFunction.getId());
								}
							}

						}

					}
				}
				
				List<Map<String,Object>> diagAccordRecords = (List<Map<String, Object>>) cureInfoMap.get("diagAccordRecords");
				if(CollectionUtils.isNotEmpty(diagAccordRecords)){
					for (Map<String, Object> diagAccordRecord : diagAccordRecords) {
						String diagCompareName = null;
						if(diagAccordRecord.get("diagCompareName")!=null){
							diagCompareName = (String) diagAccordRecord.get("diagCompareName");
							//门诊出院诊断为空或者不是符合、不符合、不肯定之一
							if(diagCompareName.equals("门诊诊断与出院诊断")){
								String diagAccordName = null;
								if(diagAccordRecord.get("diagAccordName")!=null){
									diagAccordName = (String) diagAccordRecord.get("diagAccordName");
									this.uploadMedicalModify(ModifyConstant.NO_OUTPATIENT_DISCHARGE_DIAGNOSIS_OR_ISUNDO,
											"门诊诊断与出院诊断由空修改为" + (String)diagAccordRecord.get("diagAccordName"),
											medicalFunction.getId());
									
									if(diagAccordName.equals("符合")||diagAccordName.equals("不符合")|| diagAccordName.equals("不肯定")){
										this.uploadMedicalModify(ModifyConstant.NO_OUTPATIENT_DISCHARGE_DIAGNOSIS_OR_ISUNDO,
												"门诊诊断与出院诊断由不是符合、不符合和不肯定之一修改为" + (String)diagAccordRecord.get("diagAccordName"),
												medicalFunction.getId());
									}else{
										this.addMedicalModify(ModifyConstant.NO_OUTPATIENT_DISCHARGE_DIAGNOSIS_OR_ISUNDO,
												medicalFunction.getId());
									}
									
									
								}else{
									this.addMedicalModify(ModifyConstant.NO_OUTPATIENT_DISCHARGE_DIAGNOSIS_OR_ISUNDO,
											medicalFunction.getId());
								}
							}
							
							//入院诊断与出院诊断为空或者不是符合、不符合、不肯定之一
							if(diagCompareName.equals("入院诊断与出院诊断")){
								String diagAccordName = null;
								if(diagAccordRecord.get("diagAccordName")!=null){
									diagAccordName = (String) diagAccordRecord.get("diagAccordName");
									this.uploadMedicalModify(ModifyConstant.NO_HOSPITAL_DISCHARGE_DIAGNOSIS_OR_ISUNDO,
											"入院诊断与出院诊断由空修改为" + (String)diagAccordRecord.get("diagAccordName"),
											medicalFunction.getId());
									
									if(diagAccordName.equals("符合")||diagAccordName.equals("不符合")|| diagAccordName.equals("不肯定")){
										this.uploadMedicalModify(ModifyConstant.NO_HOSPITAL_DISCHARGE_DIAGNOSIS_OR_ISUNDO,
												"入院诊断与出院诊断由不是符合、不符合和不肯定之一修改为" + (String)diagAccordRecord.get("diagAccordName"),
												medicalFunction.getId());
									}else{
										this.addMedicalModify(ModifyConstant.NO_HOSPITAL_DISCHARGE_DIAGNOSIS_OR_ISUNDO,
												medicalFunction.getId());
									}
									
									
								}else{
									this.addMedicalModify(ModifyConstant.NO_OUTPATIENT_DISCHARGE_DIAGNOSIS_OR_ISUNDO,
											medicalFunction.getId());
								}
							}
						}
						
					}
				}
				
				//病案质量未空
//				if(cureInfoMap.get("medicalRecordQualityName")!=null){
//					String medicalRecordQualityName = (String) cureInfoMap.get("medicalRecordQualityName");
//					this.uploadMedicalModify(ModifyConstant.NULL_MEDICAL_RECORD_QUALITY,
//							"病案质量由空修改为" + (String)cureInfoMap.get("medicalRecordQualityName"),
//							medicalFunction.getId());
//					//病案质量不是甲、乙、丙其中之一
//					if(medicalRecordQualityName.equals("甲")||medicalRecordQualityName.equals("乙")||medicalRecordQualityName.equals("丙")){
//						this.uploadMedicalModify(ModifyConstant.MEDICAL_RECORD_QUALITY_NOT_IN＿ABC,
//								"病案质量不为甲、乙、丙其中之一修改为" + medicalRecordQualityName,
//								medicalFunction.getId());
//					}else{
//						this.addMedicalModify(ModifyConstant.MEDICAL_RECORD_QUALITY_NOT_IN＿ABC,
//								medicalFunction.getId());
//					}
//					
//					
//				}else{
//					this.addMedicalModify(ModifyConstant.NULL_MEDICAL_RECORD_QUALITY,
//							medicalFunction.getId());
//				}
				
				//患者HIV选择为空,要选择未做、阴性或阳性之一
				if(cureInfoMap.get("hivAbResultName")!=null){
					this.uploadMedicalModify(ModifyConstant.NULL_HIV,
							"患者HIV选择为空修改为" + (String)cureInfoMap.get("hivAbResultName"),
							medicalFunction.getId());
				}else{
					this.addMedicalModify(ModifyConstant.NULL_HIV,
							medicalFunction.getId());
				}
				
				//患者HCV选择为空,要选择未做、阴性或阳性之一
				if(cureInfoMap.get("hcvAbResultName")!=null){
					this.uploadMedicalModify(ModifyConstant.NULL_HCV,
							"患者HCV选择为空修改为" + (String)cureInfoMap.get("hcvAbResultName"),
							medicalFunction.getId());
				}else{
					this.addMedicalModify(ModifyConstant.NULL_HCV,
							medicalFunction.getId());
				}
				
				//患者HBSAG选择为空,要选择未做、阴性或阳性之一
				if(cureInfoMap.get("hbsAgeResultName")!=null){
					this.uploadMedicalModify(ModifyConstant.NULL_HBSAG,
							"患者HBSAG选择为空修改为" + (String)cureInfoMap.get("hbsAgeResultName"),
							medicalFunction.getId());
				}else{
					this.addMedicalModify(ModifyConstant.NULL_HBSAG,
							medicalFunction.getId());
				}
				
				//质控日期为空或小于入院日期
				if(cureInfoMap.get("qualityControlDateTime")!=null){
					String qualityControlDateTime = (String) cureInfoMap.get("qualityControlDateTime");
					this.uploadMedicalModify(ModifyConstant.NULL_QUALITY_OR_LOWER_THAN_INHOSPITAL_TIME,
							"质控时间由空修改为" + (String)cureInfoMap.get("qualityControlDateTime"),
							medicalFunction.getId());
					//质控时间小于入院时间
					if(StringUtils.isNotEmpty(inHospitalnDateTime)){
						if(this.strToDate(qualityControlDateTime).getTime()>this.strToDate(inHospitalnDateTime).getTime()){
							this.uploadMedicalModify(ModifyConstant.NULL_QUALITY_OR_LOWER_THAN_INHOSPITAL_TIME,
									"质控时间小于入院时间修改质控时间为" + qualityControlDateTime,
									medicalFunction.getId());
						}else{
							this.addMedicalModify(ModifyConstant.NULL_QUALITY_OR_LOWER_THAN_INHOSPITAL_TIME,
									medicalFunction.getId());
						}
					}
					
				}else{
					this.addMedicalModify(ModifyConstant.NULL_QUALITY_OR_LOWER_THAN_INHOSPITAL_TIME,
							medicalFunction.getId());
				}
				
							
				
				
				
			}

		}

	}

	/**
	 * 新增MedicalModify
	 * 
	 * @param modifyCode
	 * @param medicalFunctionId
	 */
	public void addMedicalModify(String modifyCode, Integer medicalFunctionId) {
		MedModifyVo medModifyVo = medicalModifyMapper
				.getMedicalModifyByMedicalFunctionAndMedErrorCode(medicalFunctionId, modifyCode);
		if (medModifyVo == null) {
			return;
		}
		MedicalModify medicalModify = new MedicalModify();
		int medicalErrorId = medicalErrorMapper.getMedicalErrorIdByCode(modifyCode);
		medicalModify.setMedicalErrorId(medicalErrorId);
		medicalModify.setMedicalFunctionId(medicalFunctionId);
		medicalModify.setStatus(ModifyConstant.UN_MODIFY);
		medicalModifyMapper.insertSelective(medicalModify);
	}

	/**
	 * 更新MedicalModify
	 * 
	 * @param modifyCode
	 * @param modifyContent
	 * @param medicalFunctionId
	 */
	public void uploadMedicalModify(String modifyCode, String modifyContent, Integer medicalFunctionId) {
		MedModifyVo medModifyVo = medicalModifyMapper
				.getMedicalModifyByMedicalFunctionAndMedErrorCode(medicalFunctionId, modifyCode);
		if (medModifyVo == null) {
			return;
		}
		if (medModifyVo.getMedicalModifyStatus() == ModifyConstant.UN_MODIFY) {
			MedicalModify medicalModify = new MedicalModify();
			medicalModify.setMedicalErrorId(medModifyVo.getMedicalModifyId());
			medicalModify.setStatus(ModifyConstant.MODIFIED);
			medicalModify.setMedicalReviseContent(modifyContent);
			medicalModify.setMedicalFunctionId(medicalFunctionId);
			medicalModifyMapper.updateByPrimaryKeySelective(medicalModify);
		}
	}

	/**
	 * 拼装hospitalNote
	 * 
	 * @param firstGuid
	 * @param basicInfoMap
	 * @return
	 */
	public HospitalNote appendHospitalNote(String firstGuid, Map<String, Object> basicInfoMap) {
		HospitalNote hospitalNote = new HospitalNote();
		hospitalNote.setFirstGuid(firstGuid);
		hospitalNote.setName(basicInfoMap.get("patientName") == null ? null : (String) basicInfoMap.get("patientName"));
		hospitalNote.setCreateDate(new Date());
		hospitalNote.setMrId(basicInfoMap.get("mrId") == null ? null : (String) basicInfoMap.get("mrId"));
		hospitalNote.setOnlyId(basicInfoMap.get("onlyId") == null ? null : (String) basicInfoMap.get("onlyId"));
		hospitalNote.setIdNumber(basicInfoMap.get("idNumber") == null ? null : (String) basicInfoMap.get("idNumber"));
		hospitalNote.setOutHospitalOfficeCode(
				basicInfoMap.get("outDeptCode") == null ? null : (String) basicInfoMap.get("outDeptCode"));
		hospitalNote.setOutHospitalOfficeName(
				basicInfoMap.get("outDeptName") == null ? null : (String) basicInfoMap.get("outDeptName"));
		hospitalNote.setOutHospitalTypeCode(basicInfoMap.get("outHospitalTypeCode") == null ? null
				: (String) basicInfoMap.get("outHospitalTypeCode"));
		hospitalNote.setOutHospitalType(basicInfoMap.get("outHospitalTypeName") == null ? null
				: (String) basicInfoMap.get("outHospitalTypeName"));
		hospitalNote.setBirthday(basicInfoMap.get("birthday") == null ? null
				: DateUtil.strToDate((String) basicInfoMap.get("birthday")));
		hospitalNote.setOutHospitalDate(basicInfoMap.get("outHospitalDateTime") == null ? null
				: DateUtil.strToDate((String) basicInfoMap.get("outHospitalDateTime")));
		return hospitalNote;
	}

	/**
	 * 拼装更新hospitalNote
	 * 
	 * @param firstGuid
	 * @param basicInfoMap
	 * @return
	 */
	public HospitalNote appendUpdateHospitalNote(String firstGuid, Map<String, Object> basicInfoMap,
			HospitalNote hospitalNote) {
		hospitalNote.setFirstGuid(firstGuid);
		hospitalNote.setName(basicInfoMap.get("patientName") == null ? hospitalNote.getName()
				: (String) basicInfoMap.get("patientName"));
		hospitalNote.setCreateDate(new Date());
		hospitalNote
				.setMrId(basicInfoMap.get("mrId") == null ? hospitalNote.getMrId() : (String) basicInfoMap.get("mrId"));
		hospitalNote.setOnlyId(
				basicInfoMap.get("onlyId") == null ? hospitalNote.getOnlyId() : (String) basicInfoMap.get("onlyId"));
		hospitalNote.setIdNumber(basicInfoMap.get("idNumber") == null ? hospitalNote.getIdNumber()
				: (String) basicInfoMap.get("idNumber"));
		hospitalNote.setOutHospitalOfficeCode(basicInfoMap.get("outDeptCode") == null
				? hospitalNote.getOutHospitalOfficeCode() : (String) basicInfoMap.get("outDeptCode"));
		hospitalNote.setOutHospitalOfficeName(basicInfoMap.get("outDeptName") == null
				? hospitalNote.getOutHospitalOfficeName() : (String) basicInfoMap.get("outDeptName"));
		hospitalNote.setOutHospitalTypeCode(basicInfoMap.get("outHospitalTypeCode") == null
				? hospitalNote.getOutHospitalTypeCode() : (String) basicInfoMap.get("outHospitalTypeCode"));
		hospitalNote.setOutHospitalType(basicInfoMap.get("outHospitalTypeName") == null
				? hospitalNote.getOutHospitalType() : (String) basicInfoMap.get("outHospitalTypeName"));
		hospitalNote.setBirthday(basicInfoMap.get("birthday") == null ? hospitalNote.getBirthday()
				: DateUtil.strToDate((String) basicInfoMap.get("birthday")));
		hospitalNote
				.setOutHospitalDate(basicInfoMap.get("outHospitalDateTime") == null ? hospitalNote.getOutHospitalDate()
						: DateUtil.strToDate((String) basicInfoMap.get("outHospitalDateTime")));
		return hospitalNote;
	}

	/**
	 * 根据出院时间查询相关guid
	 */
	@Transactional(readOnly = true)
	@TargetDataSource(name = "ds1")
	public List<String> getFirstGuidByOutHospTime(AuditQuery auditQuery) {
		auditQuery.setStartTime(this.getPreYearDate(new Date()));
		auditQuery.setEndTime(new Date());
		return medicalRecordAuditMapper.getFirstGuidsByOutHospTime(auditQuery);
	}

	/**
	 * 获取与传入时间前一月时间
	 * 
	 * @param date
	 * @return
	 */
	public Date getPreMonthDate(Date date) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar c = Calendar.getInstance();
		// 过去一月
		c.setTime(date);
		c.add(Calendar.MONTH, -1);
		Date premonthDate = c.getTime();
		String mon = format.format(premonthDate);
		return premonthDate;
	}

	/**
	 * 获取与传入时间前一月时间
	 * 
	 * @param date
	 * @return
	 */
	public Date getPreYearDate(Date date) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar c = Calendar.getInstance();
		// 过去一月
		c.setTime(new Date());
		c.add(Calendar.YEAR, -8);
		Date premonthDate = c.getTime();
		return premonthDate;
	}

	@SuppressWarnings("unchecked")
	public Map<String, Object> readLineFile(String filePath, String fileName) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();

		try {
			File destFile = new File(filePath, fileName);
			String jsonStr = null;
			if (destFile.exists()) {
				// FileReader fr = new FileReader(destFile);
				BufferedReader br = new BufferedReader(
						new InputStreamReader(new FileInputStream(filePath + fileName), "UTF-8"));
				jsonStr = br.readLine();
				jsonMap = JsonUtils.jsonToPojo(jsonStr, Map.class);

			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		return jsonMap;
	}

	public Date strToDate(String date) throws ParseException {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return dateFormat.parse(date);
	}

	/**
	 * 病案审核查询
	 * 
	 */
	@Transactional(readOnly = true)
	@Override
	public List<Map<String, Object>> getMedicalAuditByFirstGuid(String firstGuid) {
		return medicalModifyMapper.getMedicalAuditByFirstGuid(firstGuid);
	}

}
